<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/29
 * Time: 11:27
 */
namespace App\Modules\User\Models;
use DB;
use Illuminate\Database\Eloquent\Model;

class UserAverageDetail extends Model{

    /**
     * 获取所有用户总打赏次数及总打赏金额
     * @param $uid
     * @return mixed
     */
    static function getAllUsersRewardsInfo(){
        $count = DB::table('self_reco_suc_chains as srsc')
            ->join('tasks as t', 't.id', '=', 'srsc.task_id')
            ->select(DB::raw("count('srsc.id') as rewards,sum(srsc.fee) as total_rewards"))
            ->where('srsc.pay_status','=','SUC_REWARD')
            ->first();

        $rewardTimes = $count->rewards;
        $rewardTotal = $count->total_rewards;

        return ['rewardTimes'=>$rewardTimes,'rewardTotal'=>$rewardTotal];
    }

    /**
     * 获取所有用户已发布的任务总数
     * @return mixed
     */
    static function getAllUserTaskNum() {

        $count = DB::table('tasks')
            ->select(DB::raw('count(id) as cid'))
            ->first();
        $task_num = $count->cid;

        return $task_num;
    }

    /**
     * 获取所有用户感兴趣总数
     * @return mixed
     */
    static function getAllUserPickTotal(){
        $count = DB::table('self_recommendations')
            ->select(DB::raw('count(id) as cid'))
            ->wherein('status',['PROCESSING','NORESPONSE','SUCCESS'])
            ->first();
        $interest = $count->cid;

        return $interest;
    }

    /**
     * 获取所有用户最高打赏
     * @return mixed
     */
    static function getAllMaxReward(){
        $count = DB::table('self_reco_suc_chains as srsc')
            ->leftjoin('tasks as t','t.id','=','srsc.task_id')
            ->select(DB::raw('max(srsc.fee) as max_fee'))
            ->first();
        $peakReward = $count->max_fee;
        return $peakReward;
    }

    /**
     * 获取用户处理总时长
     * @param $uid
     */
    static function handleAllDuration(){

        $sql = "select unix_timestamp(sr.view_time)-unix_timestamp(sr.created_at) as diff_time,sr.id from `self_recommendations` as sr where sr.`is_view` = 1";
        $res=DB::table(DB::raw("($sql) as tb_main"))
            ->select(DB::raw("sum(tb_main.diff_time) as sum_diff_time,count(tb_main.id) as countall"))
            ->first();
        $totalTime = $res->sum_diff_time;//->diff_time;
        $totalCount = $res->countall;
        return ['totalTime'=>$totalTime?$totalTime:0,'totalCount'=>$totalCount?$totalCount:0];
    }
}